<?php
/**
 * 团检人员MODEL
 *
 * User: kaikaic
 * Date: 2018/11/2
 * Time: 1:51
 */

namespace app\exam\model;

use app\api\model\OrderModel;
use app\exam\service\AccountService;
use app\exam\service\GroupMemberService;
use app\exam\service\OrderService;
use service\ExcelService;
use think\Db;

class GroupMemberModel {

    private $groupID;

    /**
     * 读取EXCEL文件数据
     */
    public function readExcelFile($excelFile, $groupID) {
        $excelFile = env('root_path') . $excelFile;
        $excelData = ExcelService::importExcel($excelFile);
        if (!$excelData) {
            return 0;
        }
        $this->groupID = $groupID;
        $groupinfo = Db::name('exam_group')->find($groupID);
        $importCount = 0;
        for ($i=0; $i<count($excelData); $i++) {
            $tmp = $excelData[$i];
            if (count($tmp) != 6) {
                continue;
            }

            $params = array();
            $params['user_name'] = $tmp[0];
            $params['mobile'] = $tmp[1];
            $params['gender'] = $tmp[2];
            $params['id_card'] = $tmp[3];
            $params['birthday'] = $tmp[4];
            $params['married'] = $tmp[5];

            // 数据记录
            $rs = $this->doImport($params,$groupinfo);
            if ($rs) {
                $importCount++;
            }
        }

        return $importCount;
    }
    public function doAdd($data,$groupinfo) {
        return $this->recordGroupMember($groupinfo['id'], $data['account_id'], $data['mobile'], $data['user_name'],$data['exam_indentity'],$data['birthday'],$data['married'],$groupinfo);
    }
    public function doUpdate($data,$groupinfo) {
        $userid = $data['account_id'];
        $ordersn = $data['order_sn'];
        //更新accout
        $accountdata['nick_name'] = $data['user_name'];
        $accountdata['real_name'] = $data['user_name'];
        $accountdata['mobile'] = $data['mobile'];
        $accountdata['gender'] = $data['married'];
        $accountdata['identity_card'] = $data['exam_indentity'];
       $accoutstatus =  AccountService::getInstance()->update($accountdata,$userid);

        $params['account_id'] = $userid;
        $params['province_id'] = 0;
        $params['city_id'] = 0;
        $params['birthday'] = $data['birthday'];
        $params['married'] =  $data['birthday'];
        $params['address'] = "";
        $params['post_code'] = "";
        $accoutstatus2 =  AccountService::getInstance()->update($params,$userid,'account_id',1);

        $orderdata['exam_idCard'] = $data['exam_indentity'];
        $orderdata['exam_name'] = $data['user_name'];
        $orderstatus =  OrderService::getInstance()->update($orderdata,$ordersn,'order_sn');
        if($orderstatus>=0 && $accoutstatus2>=0 && $accoutstatus>=0){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 1.生成用户账号
     * 2.生成团检人员记录
     *
     * @param $data
     * @param $groupID
     * @return bool
     */
    private function doImport($data,$groupinfo) {
        $data = $this->excelDataFilter($data);
        if (!$data) {
            return false;
        }

        $accountID = AccountService::getInstance()->importAccount($data);
        if (!$accountID) {
            return false;
        }

        return $this->recordGroupMember($this->groupID, $accountID, $data['mobile'], $data['user_name'],$data['id_card'],$data['birthday'],$data['married'],$groupinfo);
    }

    /**
     * 生成团检人员记录
     *
     * @param $groupID
     * @param $accountID
     * @param $mobile
     * @param $userName
     * @return bool
     */
    private function recordGroupMember($groupID, $accountID, $mobile, $userName,$idcard,$birthday,$married,$groupinfo) {
        $params = array();
        $params['group_id'] = $groupID;
        $params['account_id'] = $accountID;

        $record = GroupMemberService::getInstance()->single($params);
        if ($record) {
            return false;
        }

        $params['user_name'] = $userName;
        $params['mobile'] = $mobile;
        $params['status'] = 1;
        $params['create_time'] = time();
        $params['exam_indentity'] = $idcard;
        $params['birthday'] = $birthday;
        $params['married'] = $married;
        //生成订单
        $params['order_sn'] = $this->addorder($params,$groupinfo);
        return GroupMemberService::getInstance()->insert($params);
    }
    /**
     * 生成团检订单
     */
    private function addorder($data,$groupinfo){
        $accountID = $data['account_id'];           // 用户ID
        $mecID = $groupinfo['mec_id'];            // 体检中心ID
        $setID = $groupinfo['group_set_id'];         // 套餐ID
        $examDate = $groupinfo['end_date'];              // 体检日期
        $enterpriseID = $groupinfo['enterprise_id'];  ;  // 企业ID
        $price= 0;  // ID
        $name = $data['user_name'];
        $card_no =   $data['exam_indentity'];
        $groupid = $data['group_id'];  // ID
        // 订单是否存在
        $params['exam_name'] = $name;
        $params['exam_idCard'] = $card_no;
        $params['group_id'] = $groupid;
        $params['account_id'] = $accountID;
        $params['mec_id'] = $mecID;
        $params['set_id'] = $setID;
        $params['exam_date'] = $examDate;
        $params['price'] = $price;
        $params['type'] = 2;
        $orderModel = new OrderModel();
        $flag = $orderModel->isOrderExist($params);
        if ($flag) {
            $this->error("已经有当天的预约", 0);
        }
        $params['enterprise_id'] = $enterpriseID;
        $params['order_status'] = 2;
        $params['pay_status'] = 2;

        $params['create_time'] = time();

        $result = $orderModel->createTjOrder($params);
        return $result;
    }

    /**
     * 对excel数据过滤
     */
    private function excelDataFilter($data) {
        if (!$data['user_name']) {
            return array();
        }

        $gender = 0;
        if ($data['gender'] == "男") {
            $gender = 1;
        }else if ($data['gender'] == "女") {
            $gender = 2;
        }
        $data['gender'] = $gender;

        $married = 0;
        if ($data['married'] == "已婚") {
            $married = 2;
        }else if ($data['married'] == "未婚") {
            $married = 1;
        }else if ($data['married'] == "离异") {
            $married = 3;
        }
        $data['married'] = $married;

        //TODO 手机号码

        //TODO 身份证

        return $data;
    }
}

